<template>
    <div>
        <!-- 全局音频元素 -->
        <audio
            ref="audioElement"
            @timeupdate="musicStore.onTimeUpdate"
            @loadedmetadata="musicStore.onLoadedMetadata"
            @ended="musicStore.onAudioEnded"
            @error="musicStore.onAudioError"
        ></audio>

        <!-- 全局悬浮音乐控制按钮 -->
        <Teleport to="body">
            <Transition name="fade-slide">
                <div
                    v-if="musicStore.currentSong && hasEverPlayed"
                    class="floating-music-control"
                    @click="musicStore.togglePlay"
                    @mouseenter="isHovered = true"
                    @mouseleave="isHovered = false"
                >
                    <!-- 歌曲封面 -->
                    <div class="song-cover-wrapper">
                        <!-- 默认占位符 -->
                        <div v-if="!imageLoaded" class="default-cover">
                            <CustomerServiceOutlined class="default-music-icon" />
                        </div>
                        <!-- 歌曲封面图片 -->
                        <img
                            v-show="imageLoaded"
                            :src="musicStore.currentSong.cover"
                            :alt="musicStore.currentSong.name"
                            class="song-cover"
                            @load="onImageLoad"
                            @error="onImageError"
                        />
                        <!-- 播放/暂停图标覆盖层 -->
                        <div class="play-icon-overlay">
                            <PauseCircleFilled v-if="musicStore.isPlaying" class="control-icon" />
                            <PlayCircleFilled v-else class="control-icon" />
                        </div>
                        <!-- 旋转动画边框 -->
                        <div v-if="musicStore.isPlaying" class="rotating-border"></div>
                    </div>

                    <!-- 悬浮信息卡片 -->
                    <Transition name="fade">
                        <div v-if="isHovered" class="song-info-card">
                            <div class="song-name">{{ musicStore.currentSong.name }}</div>
                            <div class="song-artist">{{ musicStore.currentSong.artist }}</div>
                            <div class="control-hint">点击{{ musicStore.isPlaying ? '暂停' : '播放' }}</div>
                        </div>
                    </Transition>
                </div>
            </Transition>
        </Teleport>
    </div>
</template>

<script lang="ts" setup>
import { ref, watch, onMounted, onUnmounted } from 'vue'
import { PauseCircleFilled, PlayCircleFilled, CustomerServiceOutlined } from '@ant-design/icons-vue'
import { useMusicStore } from '@/stores'

const musicStore = useMusicStore()
const audioElement = ref<HTMLAudioElement>()
const isHovered = ref(false)
const hasEverPlayed = ref(false)
const imageLoaded = ref(false)

// 图片加载成功
const onImageLoad = () => {
    imageLoaded.value = true
}

// 图片加载失败，显示默认样式
const onImageError = () => {
    imageLoaded.value = false
}

// 监听播放状态，一旦播放过就标记为 true
watch(
    () => musicStore.isPlaying,
    (isPlaying) => {
        if (isPlaying && musicStore.currentSong) {
            hasEverPlayed.value = true
        }
    },
    { immediate: true },
)

// 监听歌曲变化，重置图片加载状态
watch(
    () => musicStore.currentSong,
    () => {
        imageLoaded.value = false
    },
)

onMounted(() => {
    if (audioElement.value) {
        musicStore.setAudioRef(audioElement.value)
    }
})

onUnmounted(() => {
    musicStore.cleanup()
})
</script>

<style scoped>
.floating-music-control {
    position: fixed;
    bottom: 100px;
    right: 30px;
    z-index: 9999;
    cursor: pointer;
    user-select: none;
}

.song-cover-wrapper {
    position: relative;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    overflow: visible;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    transition: all 0.3s ease;
}

.floating-music-control:hover .song-cover-wrapper {
    transform: scale(1.1);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}

.song-cover {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
}

.default-cover {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 50%;
}

.default-music-icon {
    font-size: 32px;
    color: white;
    opacity: 0.9;
}

.play-icon-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 50%;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.floating-music-control:hover .play-icon-overlay {
    opacity: 1;
}

.control-icon {
    font-size: 32px;
    color: white;
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.3));
}

.rotating-border {
    position: absolute;
    top: -4px;
    left: -4px;
    width: calc(100% + 8px);
    height: calc(100% + 8px);
    border-radius: 50%;
    border: 3px solid transparent;
    border-top-color: #1890ff;
    border-right-color: #1890ff;
    animation: rotate 2s linear infinite;
    pointer-events: none;
}

@keyframes rotate {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

.song-info-card {
    position: absolute;
    right: 75px;
    top: 50%;
    transform: translateY(-50%);
    background: white;
    padding: 12px 16px;
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
    white-space: nowrap;
    pointer-events: none;
}

.song-info-card::after {
    content: '';
    position: absolute;
    right: -8px;
    top: 50%;
    transform: translateY(-50%);
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 8px 0 8px 8px;
    border-color: transparent transparent transparent white;
}

.song-name {
    font-size: 14px;
    font-weight: 600;
    color: #333;
    margin-bottom: 4px;
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
}

.song-artist {
    font-size: 12px;
    color: #666;
    margin-bottom: 6px;
}

.control-hint {
    font-size: 11px;
    color: #999;
    font-style: italic;
}

/* 过渡动画 */
.fade-slide-enter-active,
.fade-slide-leave-active {
    transition: all 0.4s ease;
}

.fade-slide-enter-from {
    opacity: 0;
    transform: translateX(100px) scale(0.8);
}

.fade-slide-leave-to {
    opacity: 0;
    transform: translateX(100px) scale(0.8);
}

.fade-enter-active,
.fade-leave-active {
    transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
    opacity: 0;
}
</style>
